package com.qigao.mall.service.impl;

import com.qigao.mall.api.NoticeService;
import com.qigao.mall.commons.dto.NoticeCreateDto;
import com.qigao.mall.commons.dto.NoticeModiDto;
import com.qigao.mall.commons.dto.NoticeQueryDto;
import com.qigao.mall.commons.dto.NoticeResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.enums.StatusCharEnum;
import com.qigao.mall.jdbc.mapper.TbNoticeInfoMapper;
import com.qigao.mall.jdbc.model.TbNoticeInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Copyright (C), 2019-2019, 三人行工作室
 * FileName: NoticeServiceImpl
 * Author:   luiz
 * Date:     2019/11/10 15:41
 * Description: 公告服务实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * luiz            2019/11/10 15:41       版本号              描述
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    private TbNoticeInfoMapper tbNoticeInfoMapper;
    @Override
    public Result<NoticeResultDto> findNewNotice(String shopNo) {
       TbNoticeInfo tbNoticeInfo=tbNoticeInfoMapper.selectNewNotice(shopNo);
        if(tbNoticeInfo==null){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        NoticeResultDto noticeResultDto=new NoticeResultDto();
        BeanUtils.copyProperties(tbNoticeInfo,noticeResultDto);
        return Result.newSuccess(noticeResultDto);
    }

    @Override
    public Result<NoticeResultDto> findNoticeById(String noticeId) {
        TbNoticeInfo tbNoticeInfo=tbNoticeInfoMapper.selectByPrimaryKey(Integer.valueOf(noticeId));
        if(tbNoticeInfo==null){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
        NoticeResultDto noticeResultDto=new NoticeResultDto();
        BeanUtils.copyProperties(tbNoticeInfo,noticeResultDto);
        return Result.newSuccess(noticeResultDto);
    }

    @Override
    public Result<List<NoticeResultDto>> findNoticeList(NoticeQueryDto noticeQueryDto) {
        List<TbNoticeInfo> tbNoitceInfoList=tbNoticeInfoMapper.selectList(noticeQueryDto);
        if(tbNoitceInfoList==null || tbNoitceInfoList.size()<=0){
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
          List<NoticeResultDto> noticeResultDtos=new ArrayList<>();
        for(TbNoticeInfo tbNoticeInfo:tbNoitceInfoList){
            NoticeResultDto noticeResultDto=new NoticeResultDto();
            BeanUtils.copyProperties(tbNoticeInfo,noticeResultDto);
            noticeResultDtos.add(noticeResultDto);
        }
        return Result.newSuccess(noticeResultDtos);
    }

    @Override
    public void createNotice(NoticeCreateDto noticeCreateDto) {
        TbNoticeInfo tbNoticeInfo=new TbNoticeInfo();
        tbNoticeInfo.setNoticeDate(new Date());
        tbNoticeInfo.setNoticeContent(noticeCreateDto.getNoticeContent());
        tbNoticeInfo.setNoticeTitle(noticeCreateDto.getNoticeTitle());
        tbNoticeInfo.setStatus(StatusCharEnum.VALID.getCode());
        tbNoticeInfo.setCreateNo(noticeCreateDto.getUserNo());
        tbNoticeInfo.setModiNo(noticeCreateDto.getUserNo());
        tbNoticeInfo.setCreateTime(new Date());
        tbNoticeInfo.setModiTime(new Date());
        tbNoticeInfoMapper.insertSelective(tbNoticeInfo);
    }

    @Override
    public void modiNotice(NoticeModiDto noticeModiDto) {
        TbNoticeInfo tbNoticeInfo=new TbNoticeInfo();
        tbNoticeInfo.setId(noticeModiDto.getId());
        tbNoticeInfo.setNoticeDate(new Date());
        tbNoticeInfo.setNoticeContent(noticeModiDto.getNoticeContent());
        tbNoticeInfo.setNoticeTitle(noticeModiDto.getNoticeTitle());
        tbNoticeInfo.setStatus(StatusCharEnum.VALID.getCode());
        tbNoticeInfo.setCreateNo(noticeModiDto.getUserNo());
        tbNoticeInfo.setModiNo(noticeModiDto.getUserNo());
        tbNoticeInfo.setCreateTime(new Date());
        tbNoticeInfo.setModiTime(new Date());
        tbNoticeInfoMapper.updateByPrimaryKeySelective(tbNoticeInfo);
    }

    @Override
    public Result deleteById(String noticeId) {
        tbNoticeInfoMapper.deleteByPrimaryKey(Integer.valueOf(noticeId));
        return Result.newSuccess();
    }
}
